﻿using EC18_ESHOP.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EC18_ESHOP.Controllers
{
    public class HomeController : Controller
    {
        EShopEntities db = new EShopEntities();

        public ActionResult Index()
        {
            ////lấy logo
            //PARAMETER para = db.PARAMETER.Where(p => p.ParameterName == "logourl").FirstOrDefault();
            //ViewBag.getLogoUrl = (para != null) ? para.Value : "";

            ////lấy background color
            //para = db.PARAMETER.Where(p => p.ParameterName == "colorcode").FirstOrDefault();
            //ViewBag.getBgColor = (para != null) ? String.Format("style=\"background-color:{0}\"", para.Value) : "";
            

            ViewBag.BestSellerGroup1 = db.PRODUCT.OrderByDescending(d => d.QuantityOfSale)
                                           .Take(4).ToList();

            ViewBag.BestSellerGroup2 = db.PRODUCT.OrderByDescending(d => d.QuantityOfSale)
                                           .Skip(4).Take(4).ToList();

            ViewBag.BestViewerGroup1 = db.PRODUCT.OrderByDescending(d => d.QuantityOfView)
                                            .Take(4).ToList();

            ViewBag.BestViewerGroup2 = db.PRODUCT.OrderByDescending(d => d.QuantityOfView)
                                            .Skip(4).Take(4).ToList();
            

           //lấy ra sp đồ điện tử - điện thoại
            ViewBag.ElecProductGroup1 = db.PRODUCT
                                        .Join(db.CATEGORY, p => p.CategoryId, c => c.Category_Id, (p, c) => new { p, c })
                                        .Where(k => k.p.CategoryId == k.c.Category_Id)
                                        .Where(k => k.c.CategoryParent == 12)
                                        .OrderByDescending(k => k.p.QuantityOfView)
                                        .Select(k => k.p)
                                        .Take(4).ToList();


            ViewBag.ElecProductGroup2 = db.PRODUCT
                                       .Join(db.CATEGORY, p => p.CategoryId, c => c.Category_Id, (p, c) => new { p, c })
                                       .Where(k => k.p.CategoryId == k.c.Category_Id)
                                       .Where(k => k.c.CategoryParent == 12)   
                                       .OrderByDescending(k => k.p.QuantityOfView)
                                       .Select(k => k.p)
                                       .Skip(4).Take(4).ToList();

            //đồ gia dụng
            ViewBag.HouseHoldGroup1 = db.PRODUCT
                                      .Join(db.CATEGORY, p => p.CategoryId, c => c.Category_Id, (p, c) => new { p, c })
                                      .Where(k => k.p.CategoryId == k.c.Category_Id)
                                      .Where(k => k.c.CategoryParent == 28)
                                      .OrderByDescending(k => k.p.QuantityOfView)
                                      .Select(k => k.p)
                                      .Take(4).ToList();


            ViewBag.HouseHoldGroup2 = db.PRODUCT
                                      .Join(db.CATEGORY, p => p.CategoryId, c => c.Category_Id, (p, c) => new { p, c })
                                      .Where(k => k.p.CategoryId == k.c.Category_Id)
                                      .Where(k => k.c.CategoryParent == 28)
                                      .OrderByDescending(k => k.p.QuantityOfView)
                                      .Select(k => k.p)
                                      .Skip(4).Take(4).ToList();


            return View();
        }
    }
}
